<?php

/**
 * Json对象转成php可以识别的Json格式(json_decode可以使用的字符串)
 * @param string $js_obj_str Json对象字符串
 * @return mixed
 */
function js_obj_str2php_json_str($js_obj_str){
    return preg_replace(["/([a-zA-Z_]+[a-zA-Z0-9_]*)\s*:/", "/:\s*'(.*?)'/"], ['"\1":', ': "\1"'], $js_obj_str);
}

/**
 * 获取字符串中间的字符串
 * @param string $str 源字符串
 * @param string $headStr 开始字符串
 * @param string $footerStr 结尾字符串
 * @return bool|string
 */
function getBetweenStr($str, $headStr, $footerStr)
{
    $start = strpos($str, $headStr);
    $footer = strpos(substr($str,$start), $footerStr);
    return substr($str, $start, $footer);
}

function strToUtf8($str){
    $encode = mb_detect_encoding($str, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));
    if($encode == 'UTF-8'){
        return $str;
    }else{
        return mb_convert_encoding($str, 'UTF-8', $encode);
    }
}